package com.j2eefast.web.service;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import cn.hutool.core.date.DateUtil;
import com.j2eefast.common.core.io.PropertiesUtils;
import com.j2eefast.web.common.MailUtil;
import com.j2eefast.web.common.query.AiYangNiuReportParam;
import com.j2eefast.web.entity.AiYangNiuReport;
import com.j2eefast.web.mapper.AiYangNiuReportMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.*;

@Service
public class AiYangNiuReportService {

    @Resource
    private AiYangNiuReportMapper mapper;

    @Autowired
    private MailUtil mailUtil;

    public List<AiYangNiuReport> findList(AiYangNiuReportParam param){
        return mapper.findList(param);
    }

    /**
     * 生成满赠
     */
    public void generateMZReport(){
        AiYangNiuReportParam param = new AiYangNiuReportParam();
        param.setFilterMZ(1);

        List<AiYangNiuReport> dataList = findList(param);

        String templateFilePath = this.getClass().getClassLoader().getResource("templates/excel/templates.xlsx").getPath();
        TemplateExportParams params = new TemplateExportParams(templateFilePath);
        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("dataList",dataList);

        String filePath = generateReport(params,paramMap);

        String subject = "爱养牛满赠活动商品-" + DateUtil.format(new Date(), "yyyy-MM-dd");

        // 发送邮件服务
        mailUtil.sendReportMail(subject,filePath);
    }

    public void generateReport() {
        List<AiYangNiuReport> dataList = findList(new AiYangNiuReportParam());

        String templateFilePath = this.getClass().getClassLoader().getResource("templates/excel/templates.xlsx").getPath();
        TemplateExportParams params = new TemplateExportParams(templateFilePath);
        Map<String,Object> paramMap = new HashMap<>();

        paramMap.put("dataList",dataList);

        String filePath = generateReport(params,paramMap);

        String subject = "爱养牛全部商品-" + DateUtil.format(new Date(), "yyyy-MM-dd");

        // 发送邮件服务
        mailUtil.sendReportMail(subject,filePath);
    }

    private String generateReport(TemplateExportParams params,Map<String,Object> paramMap){
        Workbook workbook = ExcelExportUtil.exportExcel(params, paramMap);

        try {
            String filePath = PropertiesUtils.getInstance().getProperty("config.report_out_path");
            File outFile = new File(filePath + File.separator + "/templates-" + System.currentTimeMillis() + ".xlsx");

            if(!outFile.exists()){
                outFile.createNewFile();
            }

            FileOutputStream fos = new FileOutputStream(outFile);
            workbook.write(fos);
            fos.flush();
            fos.close();

            return outFile.getAbsolutePath();
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }
}
